package view.cms.desktop
{
    import flash.events.*;
    import flash.geom.*;
    
    import mx.core.*;
    
    import view.cms.desktop.events.*;

    public class ResizeHandle extends UIComponent 
    {
        private var rsmSize:Number;
        public var allowScaleX:Boolean;
        public var allowScaleY:Boolean;
        public var resizePoint:Point;
        public var vanishingPoint:Point;
        private var rsmAlpha:Number;
        private var color:uint;
		
        public function ResizeHandle()
        {
            allowScaleX = false;
            allowScaleY = false;
            rsmSize = 10;
            color = 0;
            rsmAlpha = 0.6;
            this.addEventListener(MouseEvent.ROLL_OVER, this.changeCursor);
            this.addEventListener(MouseEvent.ROLL_OUT, this.changeCursor);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.clicked);
        }
        
        private function clicked(evt:MouseEvent):void
        {
                evt.stopPropagation();
                Manipulator(this.parent).startResize(this);
        }
        
        public function update(x:Number, y:Number, w:Number=0, h:Number=0, vx:Number=0, vy:Number=0, rx:Number=0, ry:Number=0, scaleX:Boolean=false, scaleY:Boolean=false, color:uint=0):void
        {
            this.x = x;
            this.y = y;
            this.width = w;
            this.height = h;
            this.vanishingPoint = new Point(vx,vy);
            this.resizePoint = new Point(rx,ry);
            this.allowScaleX = scaleX;
            this.allowScaleY = scaleY;
            this.color = color;
            this.invalidateDisplayList();
        }
        
        private function changeCursor(evt:MouseEvent):void
        {
            if ( DrawingPage(this.parent.parent) && !DrawingPage(this.parent.parent).allLocked )
            {
                if (evt.type == MouseEvent.ROLL_OVER)
                {
                    var va:Point = Manipulator(this.parent).relativeToAbsolute(this.vanishingPoint);
                    var re:Point = Manipulator(this.parent).relativeToAbsolute(this.resizePoint);
                    var angle:Number = -Math.atan2((va.y - re.y), (va.x - re.x));
                    
                    if ( -Math.atan2(va.y - re.y, va.x - re.x) >= Math.PI * (-10 / 180) && angle <= Math.PI * (10 / 180)  || angle <= Math.PI * (-170 / 180)  || angle >= Math.PI * (170 / 180) )
                    {
                        dispatchEvent(new CursorChangeEvent(CursorChangeEvent.CHANGE, true, true, "lr"));
                    } 
                    else if ( angle >= Math.PI * (80 / 180) && angle <= Math.PI * (100 / 180) || angle <= Math.PI * (-80 / 180) && angle >= Math.PI * (-100 / 180) )
                    {
                        dispatchEvent(new CursorChangeEvent(CursorChangeEvent.CHANGE, true, true, "tb"));
                    } 
                    else if ( angle > Math.PI * (10 / 180) && angle < Math.PI * (80 / 180) || angle < Math.PI * (-100 / 180) && angle > Math.PI * (-170 / 180) )
                    {
                        dispatchEvent(new CursorChangeEvent(CursorChangeEvent.CHANGE, true, true, "bltr"));
                    } 
                    else 
                    {
                        dispatchEvent(new CursorChangeEvent(CursorChangeEvent.CHANGE, true, true, "tlbr"));
                    };
                } 
                else 
                {
                    dispatchEvent(new CursorChangeEvent(CursorChangeEvent.CHANGE, true, true, null));
                };
            };
        }
        
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth,unscaledHeight);
            
            graphics.clear();
            graphics.beginFill(color, rsmAlpha);
            graphics.lineStyle(1, color, 1);
            graphics.drawRect(0, 0, width, height);
            graphics.endFill();
        }

    }
}



